<?php 
#分類菜單
class menu{
    
    public $xml;
    
    private $xmlAry;
    
    public function __construct(){
        $this->FileClass=new FileSystem();
        $this->xml=HZHIHU_ROOT.'banner_menu.xml';
    }
    
    
	public function newsXml($array,$id){
		$xml='<?xml version="1.0" encoding="UTF-8"?>
			  <menus>
			  ';
		foreach ($array as $k=>$val){
			$val['name']=str_replace('&','&amp;',$val['name']);
			$val['name_en']=str_replace('&','&amp;',$val['name_en']);
			$xml.='<menu>';
			$xml.='
						<id>'.$val['id'].'</id>
						<pid>'.$val['pid'].'</pid>
						<name>'.$val['name'].'</name>
						<name_en>'.$val['name_en'].'</name_en>
						<showtag>'.$val['showtag'].'</showtag>
					';	
			if($val['pid']!=$id){
				$endXml.='</menu>';
			}
			if($array[$k+1]['pid']!=$val['id']){
				$xml.=$endXml;
				$endXml='';
			}
			if($array[$k+1]['pid']==$id){
				$xml.='</menu>';
			}
		}
		$xml.='</menus>';
		//var_dump($this->xml);
		$this->FileClass->writefile($this->xml,$xml);
	} 

	public function WriteXml(){
	   
		$xmlAry=$Ary=array();
		if(!is_file($this->xml)){
			exit('找不到菜单XML，请检查');
		}
		$this->xmlAry=simplexml_load_file($this->xml);
		if(sizeof($this->xmlAry->menu)==1){
			$this->xmlAry=$this->xmlAry->menu[0];
		}  
		//var_dump($this->xmlAry);		    
	}
	
	public function topMenu($showtag=1){
		$xmlAry=array();
		foreach ($this->xmlAry as $xml){
		    if(!$showtag){
		        if((int)$xml->showtag==0){
		           continue;
		        }
		    }
			$xmlAry[]=array('id'=>(int)$xml->id
						  ,'name'=>(string)$xml->name
						  ,'name_en'=>(string)$xml->name_en
						  ,'showtag'=>(string)$xml->showtag
						  ,'pid'=>(string)$xml->pid
						  );
		//var_dump($xmlAry);
		}
		//var_dump($xmlAry);
		return 	$xmlAry;
	}

	public function Hmenu(){
		$html=$this->CenMenu($this->xmlAry);	
		return $html;
	}
	
	
	public function CenMenu($xmlAry){
		foreach ($xmlAry as $k=>$xml){
    	    if($_SESSION['lan']=='en'){    	        
             // $xml->name=(string) $xml->name_en;     
    	    }
			if(isset($xml->name) && !empty($xml->name)){
				if($xml->menu){
			     	//$html.='<li><a href="javascript:void(0);">'.$xml->name.'</a><ul>'.$this->CenMenu($xml).'</ul></li>';	
			     	if((int)$xml->pid==0){		     	
			     	   $html.='<ul id="ddsubmenuside'.(int)$xml->id.'" class="ddsubmenustyle blackwhite">'.$this->CenMenu($xml).'</ul>';	
			     	}else{
			     	   // $html.='<li>'.$this->aURL($xml).'</li><ul>'.$this->CenMenu($xml).'</ul>';
			     	}
				}else{
					if((int)$xml->pid==0){
						continue;
					}
    				$html.='<li>'.$this->aURL($xml).'</li>';
				}
			}		
		}		
		return $html;
	}
	
	private  function aURL($menu){
		$url='http://'.$_SERVER['HTTP_HOST'].'/';
        $url=get_url('note',array('pid'=>(int)$menu->id));
        $title=(string)$menu->name;
		return '<a href="'.$url.'">'.$title.'</a>';
	}	
}